<?php
/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/4/28
 * Time: 16:39
 */

namespace App\Http\Controllers;

use App\Business\Menu\CreateMenu;
use App\Business\Menu\DeleteMenu;
use App\Business\Menu\GetAllMenus;
use App\Business\Menu\GetCustomMenus;
use App\Business\Menu\GetMenus;
use App\Business\Menu\GetTopMenus;
use App\Business\Menu\SortDownMenu;
use App\Business\Menu\SortUPMenu;
use App\Business\Menu\UpdateMenu;
use App\Business\Model\Menu;
use App\Business\ReturnResult;
use App\Units;

class MenusController extends Controller
{

    public function GetCustomMenus()
    {

        $business=new GetCustomMenus();
        return $business->Excute();

    }

    public function GetAllMenus()
    {
        return (new GetAllMenus())->Excute();

    }

    public function CreateMenu()
    {
        $parameters=$this->getParameters();

        if(!$parameters) return ReturnResult::ParameterError()->ToJson();

        $business=new CreateMenu();
        $business->menuName=$parameters->menuName;
        $business->isWrite=$parameters->isWrite;
        $business->isDelete=$parameters->isDelete;
        $business->menuURL=$parameters->menuURL;
        $business->parentID=$parameters->parentID;

        return $business ->Excute();
    }


    public function DeleteMenu()
    {
        $ID=request()->query("ID");


        $business=new DeleteMenu();
        $business->ID=$ID;
        return $business ->Excute();


    }

    public function UpdateMenu(){
        $parameters=$this->getParameters();

        if(!$parameters) return ReturnResult::ParameterError()->ToJson();

        $business=new UpdateMenu();
        $business->menuName=$parameters->menuName;
        $business->isWrite=$parameters->isWrite;
        $business->isDelete=$parameters->isDelete;
        $business->menuURL=$parameters->menuURL;
        $business->ID=$parameters->ID;

        return $business ->Excute();
    }

    public function SortMenu(){
        $parameters=json_decode(urldecode(request()->getContent()));

        $sortType=$parameters->sortType;

        $result=ReturnResult::Error(null,"参数错误");

        $business=null;

        switch ($sortType)
        {
            case "UP":
                $business=new SortUPMenu();
                $business->ID=$parameters->ID;
                break;

            case "Down":
                $business=new SortDownMenu();
                $business->ID=$parameters->ID;
                break;

            default:
                break;
        }

        if($business) $result=$business->Excute();

        return $result;



    }


}